package io.plus.server.command.command_handler.auth;

import io.plus.common.io;
import io.plus.common.packet.auth.CloseRequestMessage;
import io.plus.common.packet.metadata.Command;
import io.plus.common.packet.metadata.ResponseMessage;
import io.plus.common.protocol.Packet;
import io.plus.engine.pulse.AsynchronousSocketChannelContext;
import io.plus.server.Status;
import io.plus.server.UserConfiguration;
import io.plus.server.command.CommandHandler;
import io.plus.server.utils.ImKit;
import io.plus.server.utils.JsonKit;

/**
 * 关闭请求cmd命令处理器
 */
public class CloseCommandHandler implements CommandHandler {
    @Override
    public Command command() {
        return Command.close_request;
    }

    @Override
    public Packet handler(UserConfiguration userConfiguration, AsynchronousSocketChannelContext asynchronousSocketChannelContext, Packet packet) throws Exception {
        CloseRequestMessage closeRequestMessage = JsonKit.toBean(packet.getBody(), CloseRequestMessage.class);

        if (closeRequestMessage.getUserId() == null) {
            io.remove(asynchronousSocketChannelContext, "收到关闭请求");
        } else {
            String userId = closeRequestMessage.getUserId();
            io.remove(userId, "收到关闭请求!");
        }
        return ImKit.convertResponsepPacket(asynchronousSocketChannelContext, new ResponseMessage(Command.close_request, Status.C10021));
    }
}
